package cn.sh.yhk.acl.controller;

import cn.sh.yhk.acl.AuthentUser;
import cn.sh.yhk.acl.cache.UserTokenCacheInfo;
import cn.sh.yhk.acl.cache.UserTokenCacheService;
import cn.sh.yhk.acl.jwt.JwtTokenUtil;
import cn.sh.yhk.acl.service.LoginService;
import cn.sh.yhk.acl.service.UserInfoService;
import cn.sh.yhk.acl.vo.UserInfoVo;
import cn.sh.yhk.base.vo.BaseResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static cn.sh.yhk.acl.jwt.JwtTokenUtil.TOKEN_HEADER;

/**
 * @Classname Yhk
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/10/3 15:32
 * @Created by y6522
 */
@RestController
@RequestMapping
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserTokenCacheService tokenCacheService;

    @PostMapping("/user")
    public BaseResponse<UserInfoVo> getUserInfo(HttpServletRequest request) {
        String token = request.getHeader(TOKEN_HEADER);
        UserInfoVo userInfoVo = userInfoService.selectUserByUserName(JwtTokenUtil.getUserNameFromToken(token));
        return BaseResponse.success(userInfoVo);
    }
    @PostMapping("/login")
    public BaseResponse<Map<String, String>> login(@RequestBody AuthentUser authentUser) {
        Map<String, String> map = new HashMap<>();
        tokenCacheService.userTokenCacheDelete(authentUser.getUsername());
        UserTokenCacheInfo cacheInfo = tokenCacheService.getUserTokenCacheByUserName(authentUser.getUsername());
        if (cacheInfo == null) {
            map = loginService.login(authentUser);
            return BaseResponse.success(map);
        } else {
            if (cacheInfo.getMaxExpirationDate().before(new Date())) {
                tokenCacheService.userTokenCacheDelete(authentUser.getUsername());
            } else {
                map.put("username", cacheInfo.getUsername());
                map.put("token", cacheInfo.getToken());
                return BaseResponse.success(map);
//                return BaseResponse.fail("用户已登录");
            }
            map = loginService.login(authentUser);
            return BaseResponse.success(map);
        }
    }

    @GetMapping("/logout")
    public BaseResponse logout(HttpServletRequest request) {
        String token = request.getAttribute(JwtTokenUtil.TOKEN_HEADER).toString();
        String userName = JwtTokenUtil.getUserNameFromToken(token);
        tokenCacheService.userTokenCacheDelete(userName);
        return BaseResponse.success();
    }

    @PostMapping("/register")
    public BaseResponse<Map<String, String>> register(@RequestBody UserInfoVo vo) {
        int i = userInfoService.register(vo);
        if (i > 0) {
            return BaseResponse.success();
        } else {
            return BaseResponse.fail("注册失败");
        }
    }


}
